<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>传智健康</title>
        <meta name="description" content="传智健康">
        <meta name="keywords" content="传智健康">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
        <!-- 引入组件库 -->
        <script type="text/javascript" src="../js/jquery.min.js"></script>
        <script src="../js/vue.js"></script>
        <script src="../plugins/elementui/index.js"></script>
        <script src="../js/axios-0.18.0.js"></script>
        <script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=1T5gEDrbx9jiW1GbiNvNzPHYyP97lBIC"></script><!--百度地图-->
        <style>
            .avatar-uploader .el-upload {
                border: 1px dashed #d9d9d9;
                border-radius: 6px;
                cursor: pointer;
                position: relative;
                overflow: hidden;
            }
            .avatar-uploader .el-upload:hover {
                border-color: #409EFF;
            }
            .avatar-uploader-icon {
                font-size: 28px;
                color: #8c939d;
                width: 178px;
                height: 178px;
                line-height: 178px;
                text-align: center;
            }
            .avatar {
                width: 178px;
                height: 178px;
                display: block;
            }
            .datatable {
                position: relative;
                box-sizing: border-box;
                -webkit-box-flex: 1;
                width: 100%;
                max-width: 100%;
                font-size: 14px;
                color: rgb(96, 98, 102);
                overflow: hidden;
                flex: 1 1 0%;
            }
            .datatable td, .datatable th {
                padding: 12px 0;
                min-width: 0;
                -webkit-box-sizing: border-box;
                box-sizing: border-box;
                text-overflow: ellipsis;
                vertical-align: middle;
                position: relative;
                text-align: left;
            }
        </style>
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>体检机构管理<small>体检机构地址管理</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>体检机构管理</el-breadcrumb-item>
                    <el-breadcrumb-item>体检机构地址管理</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div class="app-container">
                <div class="box">
                    <div class="filter-container">
                        <el-input placeholder="体检机构名" v-model="pagination.queryString" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                        <el-button @click="findPage()" class="dalfBut">查询</el-button>
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                    </div>
                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                        <el-table-column type="index" align="center" label="序号"></el-table-column>
                        <el-table-column prop="name" label="体检机构名" align="center"></el-table-column>
                        <el-table-column prop="telephone" label="联系电话" align="center"></el-table-column>
                        <el-table-column label="经纬度" align="center">
                            <template slot-scope="scope">
                                {{scope.row.lng}}, {{scope.row.lat}}
                            </template>
                        </el-table-column>
                        <el-table-column prop="img" label="机构图片" align="center"></el-table-column>
                        <el-table-column prop="address" label="详细地址" align="center"></el-table-column>

                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination-container">
                        <el-pagination
                            class="pagiantion"
                            @current-change="handleCurrentChange"
                            :current-page="pagination.currentPage"
                            :page-size="pagination.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="pagination.total">
                        </el-pagination>
                    </div>
                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增体检机构地址" :visible.sync="dialogFormVisible">
                            <template>
                                <!--<el-tabs v-model="activeName" type="card">-->
                                        <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="体检机构名" prop="name">
                                                        <el-input v-model="formData.name"/>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="联系电话">
                                                        <el-input v-model="formData.telephone"/>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="24">
                                                    <el-form-item label="上传体检机构地址图片">
                                                        <el-upload
                                                                class="avatar-uploader"
                                                                action="/setmeal/upload.do"
                                                                :auto-upload="autoUpload"
                                                                name="imgFile"
                                                                :show-file-list="false"
                                                                :on-success="handleAvatarSuccess"
                                                                :before-upload="beforeAvatarUpload">
                                                            <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                                        </el-upload>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="24">
                                                    <el-form-item label="经纬度">
                                                        <el-input id="addLonAndLat" v-model="formData.lonAndLat"></el-input>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-form-item label="详细地址">
                                                    <el-input id="addAddressDetail" style="width: 300px;" v-model="formData.address"></el-input>
                                                    <el-button type="primary" size="mini" round icon="el-icon-location" @click="addShow()">设置定位</el-button>
                                                </el-form-item>
                                            </el-row>
                                            <div v-show="dialogBaiDuMapAdd">
                                                <div id="addMap" style="width: 500px;height:200px;"></div>
                                                <p>点击地图展示详细地址</p>
                                                <div v-show=false id="r">
                                                    经度: <input id="addLongitude" type="text" style="width:100px; margin-right:10px;" />
                                                    纬度: <input id="addLatitude" type="text" style="width:100px; margin-right:10px;" />
                                                </div>
                                            </div>
                                        </el-form>
                            </template>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handleAdd()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                    <!-- 编辑标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="编辑体检机构地址" :visible.sync="dialogFormVisible4Edit">
                            <template>
                                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                        <el-row>
                                            <el-col :span="12">
                                                <el-form-item label="体检机构名" prop="name">
                                                    <el-input v-model="formData.name" disabled />
                                                </el-form-item>
                                            </el-col>
                                            <el-col :span="12">
                                                <el-form-item label="联系电话">
                                                    <el-input v-model="formData.telephone"/>
                                                </el-form-item>
                                            </el-col>
                                        </el-row>
                                        <el-row>
                                            <el-col :span="24">
                                                <el-form-item label="上传体检机构地址图片">
                                                    <el-upload
                                                            class="avatar-uploader"
                                                            action="/setmeal/upload.do"
                                                            :auto-upload="autoUpload"
                                                            name="imgFile"
                                                            :show-file-list="false"
                                                            :on-success="handleAvatarSuccess"
                                                            :before-upload="beforeAvatarUpload">
                                                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                                    </el-upload>
                                                </el-form-item>
                                            </el-col>
                                        </el-row>
                                        <el-row>
                                            <el-col :span="24">
                                                <el-form-item label="经纬度">
                                                    <el-input id="lonAndLat" v-model="formData.lonAndLat"></el-input>
                                                </el-form-item>
                                            </el-col>
                                        </el-row>
                                        <el-row>
                                                <el-form-item label="详细地址">
                                                    <el-input id="addressDetail" style="width: 300px;" v-model="formData.address"></el-input>
                                                    <el-button type="primary" size="mini" round icon="el-icon-location" @click="isShow()">设置定位</el-button>
                                                </el-form-item>
                                        </el-row>
                                        <div v-show="dialogBaiDuMap">
                                            <div id="allmap" style="width: 500px;height:200px;"></div>
                                            <p>点击地图展示详细地址</p>
                                            <div v-show=false id="r-result">
                                                经度: <input id="longitude" type="text" style="width:100px; margin-right:10px;" />
                                                纬度: <input id="latitude" type="text" style="width:100px; margin-right:10px;" />
                                            </div>
                                        </div>
                                    </el-form>
                            </template>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                                <el-button type="primary" @click="handleEdit()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                </div>
            </div>
        </div>
    </body>

    <script>
        var vue = new Vue({
            el: '#app',
            data:{
                autoUpload:true,//自动上传
                imageUrl:null,//模型数据，用于上传图片完成后图片预览
                activeName:'first',//添加/编辑窗口Tab标签名称
                lng:'',
                lat:'',
                lonAndLat:'',
                address:'',
                pagination: {//分页相关属性
                    currentPage: 1,
                    pageSize:10,
                    total:100,
                    queryString:null,
                },
                dataList: [],//列表数据
                formData: {},//表单数据
                dialogFormVisible: false,//控制添加窗口显示/隐藏
                dialogFormVisible4Edit:false,//编辑窗口的显示/隐藏
                rules: {//校验规则
                    name: [{ required: true, message: '体检机构名称为必填项', trigger: 'blur' }],
                },
                dialogBaiDuMap:false,//百度地图的显示/隐藏
                dialogBaiDuMapAdd:false,//添加表单中的百度地图
            },
            created() {
                this.findPage();
            },
            methods: {

                //文件上传成功后的钩子，response为服务端返回的值，file为当前上传的文件封装成的js对象
                handleAvatarSuccess(response, file) {
                    this.imageUrl = "http://qbj8ctefd.bkt.clouddn.com/" + response.data;
                    //http://q7s2roupm.bkt.clouddn.com/1a5d6044-e138-4327-a1c0-dca87f6bb8509.jpg
                    this.$message({
                        message: response.message,
                        type: response.flag ? 'success' : 'error'
                    });
                    //设置模型数据（图片名称），后续提交ajax请求时会提交到后台最终保存到数据库
                    this.formData.img = response.data;
                },
                //上传图片之前执行
                beforeAvatarUpload(file) {
				  const isJPG = file.type === 'image/jpeg';
				  const isLt2M = file.size / 1024 / 1024 < 2;
				  if (!isJPG) {
					this.$message.error('上传套餐图片只能是 JPG 格式!');
				  }
				  if (!isLt2M) {
					this.$message.error('上传套餐图片大小不能超过 2MB!');
				  }
				  return isJPG && isLt2M;
                },
                //编辑
                handleEdit() {
                    this.formData.lng=lng;
                    this.formData.lat=lat;
                    this.formData.lonAndlat=lonAndLat;
                    this.formData.address=address;
                    //进行表单的校验
                    this.$refs['dataEditForm'].validate((valid)=>{
                        if(valid){
                            //表单校验通过,可以提交数据
                            axios.post("/address/edit.do",this.formData).then((res)=>{
                                //隐藏编辑窗口
                                this.dialogFormVisible4Edit=false;
                                //修改成功
                                if(res.data.flag){
                                    this.$message({
                                        type:'success',
                                        message:res.data.message
                                    })
                                }else{
                                    //修改失败
                                    this.message.error(res.data.message)
                                }
                            }).finally(()=>{
                                this.findPage();
                            })
                        }else{
                            //表单校验不通过
                            this.$message.error("表单数据校验失败!");
                            return false;
                        }
                    });
                },
                //添加
                handleAdd () {
                    this.formData.lng=lng;
                    this.formData.lat=lat;
                    this.formData.lonAndlat=lonAndLat;
                    this.formData.address=address;
                    //进行表单的校验
                    this.$refs['dataAddForm'].validate((valid)=>{
                        if(valid){
                            //表单校验通过,可以提交数据
                            axios.post("/address/add.do?",this.formData).then((res)=>{
                                //如果添加成功隐藏菜单
                                this.dialogFormVisible=false;
                                if(res.data.flag){
                                    this.$message({
                                        type:'success',
                                        message:res.data.message
                                    });
                                } else {
                                    this.$message.error(res.data.message);
                                }
                            }).finally(() => {
                                this.findPage();
                            });
                        }else{
                            //表单校验不通过
                            this.$message.error("表单数据校验失败!");
                            return false;
                        }
                    });
                },
                //分页查询
                findPage() {
                    var parm = {
                        currentPage: this.pagination.currentPage,
                        pageSize: this.pagination.pageSize,
                        queryString: this.pagination.queryString
                    };
                    axios.post("/address/findPage.do" , parm).then((response)=>{
                        this.dataList = response.data.rows;
                        this.pagination.total = response.data.total;
                    }).catch((r)=>{
                        this.showMessage(r);
                    });
                },
                // 重置表单
                resetForm() {
                    this.formData={};
                    this.imageUrl=null;
                    this.dialogBaiDuMapAdd=false;
                    this.dialogBaiDuMap=false;
                },
                // 弹出添加窗口
                handleCreate() {
                    this.resetForm();
                    this.dialogFormVisible=true;
                },
                // 弹出编辑窗口
                handleUpdate(row) {
                    let _this = this;
                    this.dialogFormVisible4Edit=true;
                    axios.get("/address/findById.do?id="+row.id).then((res)=>{
                        if(res.data.flag){
                            this.formData=res.data.data;
                            _this.$set(_this.formData,'lonAndLat',res.data.data.lng+","+res.data.data.lat);
                            this.imageUrl="http://qbj8ctefd.bkt.clouddn.com/"+res.data.data.img;

                        }else {
                            this.$message.error("获取数据失败，请刷新当前页面");
                        }
                    });
                },
                //切换页码
                handleCurrentChange(currentPage) {
                    //重新设置最新的页码
                    this.pagination.currentPage=currentPage;
                    //重新调用findPage方法进行分页查询
                    this.findPage();
                },
                // 删除
                handleDelete(row) {
                    // row其实是一个json对象
                    this.$confirm("你确定要删除当前数据吗","提示",{
                        confirmButtonText:'确定',
                        cancelButtonText:'取消',
                        type:"warning"
                    }).then(() => {
                        //点击确定之后
                        axios.get("/address/delete.do?id="+ row.id).then((res)=>{
                            //删除失败
                            if(!res.data.flag){
                                //删除失败
                                this.$message.error(res.data.message);
                            }else {
                                //删除成功
                                this.$message({
                                    type:'success',
                                    message:res.data.message
                                });
                                this.findPage();
                            }
                        });
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
                },
                isShow(){
                    this.dialogBaiDuMap=!this.dialogBaiDuMap;
                    this.createMap();
                },
                addShow(){
                    this.dialogBaiDuMapAdd=!this.dialogBaiDuMapAdd;
                    this.createAddMap();
                },
                createMap() {
                    // 百度地图API功能
                    var map = new BMap.Map("allmap");
                    var point = new BMap.Point(118.803, 32.076);
                    map.centerAndZoom(point,12);
                    var geoc = new BMap.Geocoder();
                    map.addEventListener("click", function(e){
                        var pt = e.point;
                        geoc.getLocation(pt, function(rs){
                            var addComp = rs.addressComponents;
                            document.getElementById("longitude").value=pt.lng;
                            document.getElementById("latitude").value=pt.lat;
                            document.getElementById("lonAndLat").value=pt.lng+','+pt.lat;
                            document.getElementById("addressDetail").value=""+addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
                            lng=pt.lng;
                            lat=pt.lat;
                            lonAndLat=pt.lng+','+pt.lat;
                            address=""+addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
                            theLocation();
                        });

                    });
                    // 添加带有定位的导航控件
                    var navigationControl = new BMap.NavigationControl({
                        // 靠左上角位置
                        anchor: BMAP_ANCHOR_TOP_LEFT,
                        // LARGE类型
                        type: BMAP_NAVIGATION_CONTROL_LARGE,
                        // 启用显示定位
                        enableGeolocation: true
                    });
                    map.addControl(navigationControl);
                    // 添加定位控件
                    var geolocationControl = new BMap.GeolocationControl();
                    geolocationControl.addEventListener("locationSuccess", function(e){
                        // 定位成功事件
                        var address = '';
                        address += e.addressComponent.province;
                        address += e.addressComponent.city;
                        address += e.addressComponent.district;
                        address += e.addressComponent.street;
                        address += e.addressComponent.streetNumber;
                        alert("当前定位地址为：" + address);
                    });
                    geolocationControl.addEventListener("locationError",function(e){
                        // 定位失败事件
                        alert(e.message);
                    });
                    map.addControl(geolocationControl);
                    // 用经纬度设置地图中心点
                    function theLocation(){
                        if(document.getElementById("longitude").value != "" && document.getElementById("latitude").value != ""){
                            map.clearOverlays();
                            var new_point = new BMap.Point(document.getElementById("longitude").value,document.getElementById("latitude").value);
                            var marker = new BMap.Marker(new_point);  // 创建标注
                            map.addOverlay(marker);              // 将标注添加到地图中
                            map.panTo(new_point);
                        }
                    }

                },
                createAddMap() {
                    // 百度地图API功能
                    var addMap = new BMap.Map("addMap");
                    var point = new BMap.Point(118.803, 32.076);
                    addMap.centerAndZoom(point,12);
                    var geoc = new BMap.Geocoder();
                    addMap.addEventListener("click", function(e){
                        var addPt = e.point;
                        geoc.getLocation(addPt, function(rs){
                            var addComp_a = rs.addressComponents;
                            document.getElementById("addLongitude").value=addPt.lng;
                            document.getElementById("addLatitude").value=addPt.lat;
                            document.getElementById("addLonAndLat").value=addPt.lng+','+addPt.lat;
                            document.getElementById("addAddressDetail").value=""+addComp_a.province + addComp_a.city + addComp_a.district + addComp_a.street + addComp_a.streetNumber;
                            lng=addPt.lng;
                            lat=addPt.lat;
                            lonAndLat=addPt.lng+','+addPt.lat;
                            address=""+addComp_a.province + addComp_a.city + addComp_a.district + addComp_a.street + addComp_a.streetNumber;
                            theAddLocation();
                        });

                    });
                    // 添加带有定位的导航控件
                    var navigationControl = new BMap.NavigationControl({
                        // 靠左上角位置
                        anchor: BMAP_ANCHOR_TOP_LEFT,
                        // LARGE类型
                        type: BMAP_NAVIGATION_CONTROL_LARGE,
                        // 启用显示定位
                        enableGeolocation: true
                    });
                    addMap.addControl(navigationControl);
                    // 添加定位控件
                    var geolocationControl = new BMap.GeolocationControl();
                    geolocationControl.addEventListener("locationSuccess", function(e){
                        // 定位成功事件
                        var address = '';
                        address += e.addressComponent.province;
                        address += e.addressComponent.city;
                        address += e.addressComponent.district;
                        address += e.addressComponent.street;
                        address += e.addressComponent.streetNumber;
                        alert("当前定位地址为：" + address);
                    });
                    geolocationControl.addEventListener("locationError",function(e){
                        // 定位失败事件
                        alert(e.message);
                    });
                    addMap.addControl(geolocationControl);
                    // 用经纬度设置地图中心点
                    function theAddLocation(){
                        if(document.getElementById("addLongitude").value != "" && document.getElementById("addLatitude").value != ""){
                            addMap.clearOverlays();
                            var new_Addpoint = new BMap.Point(document.getElementById("addLongitude").value,document.getElementById("addLatitude").value);
                            var addMarker = new BMap.Marker(new_Addpoint);  // 创建标注
                            addMap.addOverlay(addMarker);              // 将标注添加到地图中
                            addMap.panTo(new_Addpoint);
                        }
                    }

                },
            }
        })
    </script>
</html>
